package com.huaxiafinance.management.service.impl;

import com.huaxiafinance.management.dto.request.AdvertisementDto;
import com.huaxiafinance.management.dto.request.QueryAdversitementDto;
import com.huaxiafinance.management.mapper.YfqAdvertisementMapper;
import com.huaxiafinance.management.mapper.YfqDictionaryMapper;
import com.huaxiafinance.management.mapper.YfqImgMapper;
import com.huaxiafinance.management.model.YfqAdvertisement;
import com.huaxiafinance.management.model.YfqImg;
import com.huaxiafinance.management.service.YfqAdvertisementService;
import com.huaxiafinance.management.util.QiNiuUtil;
import com.huaxiafinance.management.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by xieWei on 2017/11/07
 */
@Service("yfqAdvertisementService")
@SuppressWarnings("SpringJavaAutowiringInspection")
public class YfqAdvertisementServiceImpl implements YfqAdvertisementService {

    private static final Integer ACTIVE=15;
    @Autowired
    private YfqAdvertisementMapper yfqAdvertisementMapper;
    @Autowired
    private YfqImgMapper yfqImgMapper;
    @Autowired
    private YfqDictionaryMapper yfqDictionaryMapper;

    /**
     * 查询广告列表
     * @param dto
     * @return
     * @throws Exception
     */
    @Override
    public List<YfqAdvertisement> queryYfqAdversitement(QueryAdversitementDto dto) throws Exception{
        return  yfqAdvertisementMapper.selectYfqAdversitement(dto);
    }

    /**
     * 修改广告详情
     * @param dto
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public boolean updateByPrimaryKey(AdvertisementDto dto) throws Exception{
        YfqAdvertisement yfqAdvertisement = new YfqAdvertisement();
        yfqAdvertisement.setId(dto.getId());
         yfqAdvertisement.setCityId(dto.getCityId());
        if(dto.getJumpType()!=null && dto.getJumpType()!=0){yfqAdvertisement.setJumpType(dto.getJumpType());}
        if(dto.getJumpParam()!=null && dto.getJumpParam()!=""){ yfqAdvertisement.setJumpParam(dto.getJumpParam());}
        if(dto.getTitle()!=null && dto.getTitle()!=""){ yfqAdvertisement.setTitle(dto.getTitle());}
        //图片判断
        if(dto.getPicture()!=null && dto.getPicture()!=""){
            if(!"http".equals(dto.getPicture().substring(0,4))){
                yfqAdvertisement.setImgUrl(QiNiuUtil.url(dto.getPicture()));
            }
        }
        if(dto.getSort()!=null && dto.getSort()!=0){ yfqAdvertisement.setSort(dto.getSort());}
        yfqAdvertisementMapper.updateByPrimaryKeySelective(yfqAdvertisement);
        return true;
    }

    /**
     * 添加广告
     * @param dto
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public boolean insertYfqAdvertisement(AdvertisementDto dto)throws Exception{
        YfqAdvertisement yfqAdvertisement = new YfqAdvertisement();
        yfqAdvertisement.setId(UUIDUtil.getUUID());
        yfqAdvertisement.setCityId(dto.getCityId());
        yfqAdvertisement.setJumpType(dto.getJumpType());
        yfqAdvertisement.setJumpParam(dto.getJumpParam());
        yfqAdvertisement.setTitle(dto.getTitle());
        yfqAdvertisement.setCreateDate(new Date());
        yfqAdvertisement.setStartTime(dto.getStartTime());
        yfqAdvertisement.setEndTime(dto.getEndTime());
        yfqAdvertisement.setImgUrl(QiNiuUtil.url(dto.getPicture()));
        yfqAdvertisement.setLocationType(dto.getLocationType());
        yfqAdvertisement.setCreateId("");
        //默认排序
        yfqAdvertisement.setSort(1);
        //默认下架
        yfqAdvertisement.setIsEnabled(0);
        yfqAdvertisementMapper.insertSelective(yfqAdvertisement);
        return true;
    }

    /**
     * 修改广告
     * @param id
     * @param state
     * @return
     */
    @Override
    public boolean updateState(String id, Integer state) {
        YfqAdvertisement yfqAdvertisement=new YfqAdvertisement();
        yfqAdvertisement.setId(id);
        yfqAdvertisement.setIsEnabled(state);
        yfqAdvertisementMapper.updateByPrimaryKeySelective(yfqAdvertisement);
        return true;
    }

    /**
     * 置顶
     * @param id 广告id
     * @return
     */
    @Override
    public boolean updateTop(String id) {
        YfqAdvertisement yfq = yfqAdvertisementMapper.selectSort();
        int sort = yfq.getSort();
        YfqAdvertisement yfqAdvertisement=new YfqAdvertisement();
        yfqAdvertisement.setId(id);
        yfqAdvertisement.setSort(sort+1);
        yfqAdvertisementMapper.updateByPrimaryKeySelective(yfqAdvertisement);
        return true;
    }

    /**
     * 计算广告条数
     * @param locationType
     * @return
     */
    @Override
    public int count(Integer locationType) {
        return yfqAdvertisementMapper.count(locationType);
    }

    /**
     * 查询广告单个详情
     * @param id 广告id
     * @return
     */
    @Override
    public YfqAdvertisement selectYfqAdvById(String id) {
        return yfqAdvertisementMapper.selectByPrimaryKey(id);
    }


    /**
     * 活动banner图片修改和添加
     * @param url 图片链接
     */
    @Override
    @Transactional
    public  void updateOrAddImg(String url) {
       YfqImg yfqImg=yfqImgMapper.selectByActivityBanner();
       if(yfqImg!=null){
           yfqImgMapper.updateBannerImg(url);
       }else {
           YfqImg y=new YfqImg();
           y.setId(UUIDUtil.getUUID());
           // 活动banner的代码
           y.setType(ACTIVE);
           y.setTypeParam("活动banner的入口图片");
           y.setImgUrl(url);
           //默认排序
           y.setSort(1);
           yfqImgMapper.insert(y);
       }
    }

    /**
     * 获得获得活动banner 入口图片
     * @return
     */
    @Override
    public YfqImg getBannerImg() {

        return yfqImgMapper.selectByActivityBanner();
    }

    /**
     * 获得攻略url
     * @return
     */
    @Override
    public Map getRaidersUrl() {

        return yfqDictionaryMapper.getRaidersUrl();
    }

    /**
     *  修改攻略url
     * @param url
     */
    @Override
    public void updateRaidersUrl(String url) {
        yfqDictionaryMapper.updateRaidersUrl(url);
    }
}
